package com.dayuanit.dy15.lesson26.demo1;

import java.io.UnsupportedEncodingException;

public class Test {
    public static void main(String[] args) throws Exception {
        //String 常用的方法

        String msg = "hello";

        System.out.println(msg.length());//字符串长度
        System.out.println(msg.toUpperCase());//HELLO 大写
        System.out.println(msg);//hello
        System.out.println(msg.toLowerCase());//小写

        System.out.println(msg.replace("e", "x"));//替换字符串
        System.out.println(msg.replace("e", ""));//替换字符串


        System.out.println(msg.equals("hello"));//true
        System.out.println(msg.equals("Hello"));//false
        System.out.println(msg.equalsIgnoreCase("Hello"));//true

        System.out.println(msg.indexOf("a"));//返回检索字符串的下标，如果没有检索到，则返回-1
        System.out.println(msg.indexOf("el"));//1
        System.out.println(msg.indexOf("elo"));//-1
        System.out.println(msg.indexOf("ell"));//1

        System.out.println(msg.substring(2));//截取字符串 llo
        System.out.println(msg.substring(2,4));//ll

        int beign = msg.indexOf("ell");
        int end = beign + 3;
        System.out.println(msg.substring(beign, end));


        System.out.println( msg.contains("el"));//true

        byte[] bytes = msg.getBytes();
        System.out.println(bytes.length);

        String name = "大";
        System.out.println(name.getBytes().length);
        System.out.println(name.getBytes("UTF-8").length);
        System.out.println(name.getBytes("GBK").length);
        System.out.println(name.getBytes("gb2312").length);

        char[] chars = msg.toCharArray();
        for (char x : chars) {
            System.out.println(x);
        }
        chars[1] = 'x';//hxllo

        System.out.println(msg);

        name = " a  tom bob      ";
        System.out.println(name.length());
        System.out.println( name.trim());//将字符串的前后空格去掉
        System.out.println(name.length());
        System.out.println(name.replace(" ", ""));

       name = "a,b,c,d";
       String[] split = name.split(",");
       for (String xx : split) {
           System.out.println(xx);
       }

//       String.format()
        int x = 3;
        int y = 6;
        String poingInfo = "坐标：["+x+", "+y+"]";
        System.out.println(poingInfo);

        String poingInfo2 = "坐标：[%s, %s]";
        System.out.println(String.format(poingInfo2, x, y));

        int id = 999;
        String username = "tom";
        String sql = "select * from user where id ="+id+" and username='"+username+"'";
        String sql2 = "select * from user where id=%s and username='%s'";
        sql2 = String.format(sql2, id, username);
        System.out.println(sql2);
    }
}
